<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\AwardModel;

class AwardController extends BaseController {
	
	const OPERATORNUM = 3;
	
	/* 实例化模型 */
	protected function initobj(){
		return new AwardModel();
	}
		
	/* 获取奖品规则 */
	public function index(){
		$userinfo = session('userinfo');
		$ward 	= D(C('TABLE_AWARD'));
		$where['typeid'] = 0;
		if($userinfo['type'] == 0) $where['userid'] = $userinfo['id'];
		$count 	= $ward->where($where)->count();
		//每页显示条数
		$num 	= 3;
		$page 	= new \Think\Page($count,$num);
		$obj 	= pagestyle($page);
		$show 	= $obj -> show();
		$res 	= $ward -> where($where) -> order('awarddate desc') -> limit($obj->firstRow.','.$obj->listRows) -> select();
		foreach($res as &$r){
			$r['info'] = $this -> getaward($r['id']);
		}
		$this 	-> assign('awardlist',$res);
		$this 	-> assign('page',$show);
		
		$award 	= $this->initobj();
		$this 	-> assign('teltopup', $award::teltopup());
		$this 	-> assign('awardtype', $award::awardtype());
		$this 	-> assign('flowl', $award::flowl());
		$this 	-> display();
	}
	
	public function getaward($typeid){
		$ward 	= D(C('TABLE_AWARD'));
		$res 	= $ward -> where(array('typeid'=>$typeid)) -> select();
		foreach($res as &$r){
			$r['flowinfo'] = $this -> get_flow_rule($r['id']);
		}
		return $res;
	}
	
	/* 根据流量类型获取三大营运商的流量奖品规则 */
	protected function get_flow_rule($awardid = null){
		if(empty($awardid)) return false;
		$awardflow = D(C('TABLE_AWARDFLOW'));
		$res = $awardflow -> where(array('award_id' => $awardid)) -> select();
		foreach ($res as $v){
			$result .= $v['flow_id'].',';
		}
		$result = rtrim($result,',');
		return $result;		
	}

	
// 	public function editaward(){
// 		$id 		= I('post.id');
// 		$awardname 	= I('post.awardname');
// 		$awardnum 	= I('post.awardnum');
// 		$awardkind 	= I('post.awardkind');
// 		$awardimg 	= I('post.awardimg');
// 		$u_hbmin 	= I('post.u_hbmin');
// 		$u_hbmax 	= I('post.u_hbmax');
// 		$awardteltopup 	= I('post.awardteltopup');
// 		if(empty($awardimg)) $awardimg = null;
// 		if(empty($u_hbmin)) $u_hbmin = null;
// 		if(empty($u_hbmax)) $u_hbmax = null;
// 		//$awardimg = I('post.awardimg');
// 		$award = D(C('TABLE_AWARD'));
// 		$data['awardname'] 	= $awardname;
// 		$data['awardnum'] 	= $awardnum;
// 		$data['awardkind'] 	= $awardkind;
// 		$data['awardimg'] 	= $awardimg;
// 		$data['u_hbmin'] 	= $u_hbmin;
// 		$data['u_hbmax'] 	= $u_hbmax;
// 		$data['awardteltopup'] 	= $awardteltopup;
// 		$data['userid'] 	= session('userinfo')['id'];
// 		//$data['awardimg'] = $awardimg;
// 		$res = $award->where(array('id'=>$id))->save($data);
// 		if($res==0 || $res){
// 			echo 1;//修改成功
// 		}else{
// 			echo -1;//修改失败
// 		}
// 	}

	/* 修改奖品 */
	public function editaward(){
		//获取奖品分类
		$awardkind 		= I('post.awardkind');
		$awardname 		= trim(I('post.awardname'));
		$awardnum 		= trim(I('post.awardnum'));
		if(empty($awardname)) $this 				-> error('奖项名称不能为空','',1);
		if(empty($awardnum) || $awardnum < 1) $this -> error('奖品数量填写不正确','',1);
		$id 			= I('post.id');
		$awardteltopup 	= I('post.awardteltopup');
		$award 			= D(C('TABLE_AWARD'));
		// 启动事务
		$award->startTrans();
		$award->create();
		$res = $award->where(array('id'=>$id))->save($data);
		if($res==0 || $res){
			// 如果是流量充值奖品则插入流量规则表
			if($awardkind == 4){
				foreach(I('post.lt_eck') as $v) $lt_eck .= $v.",";
				foreach(I('post.yd_eck') as $v) $yd_eck .= $v.",";
				foreach(I('post.dx_eck') as $v) $dx_eck .= $v.",";
				if(!empty(rtrim($lt_eck,',')) && !empty(rtrim($yd_eck,',')) && !empty(rtrim($dx_eck,','))){
					$flow_id = array(rtrim($lt_eck,','), rtrim($yd_eck,','), rtrim($dx_eck,','));
					$this->flow_edit($id, $flow_id) ? $award -> commit() : $award -> rollback();
				}else{
					$this->error('三大运营商都必须勾选','',2);
				}
			}else{
				$res ? $award -> commit() : $award -> rollback();
			}
				
			header('Location:'.U('index'));
		}else{
			$this->error('修改失败','',1);
		}
	}

	/* 修改运营商充值ID信息 */
	protected function flow_edit($award_id, $flow_id){
		$award_flow 			= D('awardflow');
		for ($i = 1; $i <= self::OPERATORNUM; $i++){
			$data['flow_id']	= $flow_id[$i-1];
			$flow_result 		= $award_flow -> where(array('operator_id' => $i)) -> save($data);
		}				
		return $flow_result || $flow_result == 0 ? true : false;
	}
	
	/* 添加运营商充值ID信息 */
	protected function flow_add($award_id, $flow_id){
		$award_flow 				= D('awardflow');
		for ($i = 1; $i <= self::OPERATORNUM; $i++){
			$dataList[] = array(
					'award_id'		=> $award_id,
					'flow_id'		=> $flow_id[$i-1],
					'operator_id'	=> $i,
			);
		}
		$flow_result 				= $award_flow -> addAll($dataList);
		return $flow_result ? true : false;
	}

	/* 添加到某个规则下的奖品 */
	public function addaward(){
		//获取奖品分类
		$awardkind 		= I('post.awardkind');
		if(empty(trim(I('post.awardname')))) $this->error('信息填写不完整','',1);
		$typeid 		= I('post.typeid');
		$awardteltopup 	= I('post.awardteltopup');
		$award 			= D(C('TABLE_AWARD'));
		// 启动事务
		$award->startTrans();
		$award->create();
		$award->typeid 			= $typeid;
		$award->userid 			= session('userinfo')['id'];
		$award->awardteltopup 	= $awardteltopup;
		$res = $award->add();
		if($res==0 || $res){
			// 如果是流量充值奖品则插入流量规则表
			if($awardkind == 4){
				foreach(I('post.lt_eck') as $v) $lt_eck .= $v.",";
				foreach(I('post.yd_eck') as $v) $yd_eck .= $v.",";
				foreach(I('post.dx_eck') as $v) $dx_eck .= $v.",";
				if(!empty(rtrim($lt_eck,',')) && !empty(rtrim($yd_eck,',')) && !empty(rtrim($dx_eck,','))){
					$flow_id = array(rtrim($lt_eck,','), rtrim($yd_eck,','), rtrim($dx_eck,','));
					$this->flow_add($res, $flow_id) ? $award -> commit() : $award -> rollback();
				}else{
					$this->error('三大运营商都必须勾选','',2);
				}
			}else{
				$res ? $award -> commit() : $award -> rollback();
			}
			
			header('Location:'.U('index'));
		}else{
			$this->error('添加失败','',1);
		}
	}
	
	
	/* 删除奖品 */
	public function deleteaward(){
		$id 	= I('post.id');
		$award 	= D(C('TABLE_AWARD'));
		// 启动事务
		$award  -> startTrans();
		$res 	= $award->delete($id);
		
		$awardflow 	= D(C('TABLE_AWARDFLOW'));
		$count 		= $awardflow -> where(array('award_id' => $id)) -> count();
		if($count){
			$res1 	= $awardflow -> where(array('award_id' => $id)) -> delete();
			$res1 ? $award -> commit() : $award -> rollback();
		}else{
			$res ? $award -> commit() : $award -> rollback();
		}
		echo $res ? 1 : -1;
	}

	/* 添加规则标题 */
	public function addtitle(){
		$ruletitle 	= I('post.ruletitle');
		$ruledes 	= I('post.ruledes');
		$award 		= D(C('TABLE_AWARD'));
		$data['typeid'] 	= 0;
		$data['typename'] 	= $ruletitle;
		$data['awarddes'] 	= $ruledes;
		$data['awarddate'] 	= date("Y-m-d H:i:m");
		$data['userid'] 	= session('userinfo')['id'];
		$res = $award->add($data);
		echo $res ? 1 : -1;
	}

	/* 删除规则 */
	public function deleterule(){
		$id 	= I('post.id');
		$award 	= D(C('TABLE_AWARD'));
		$res 	= $award->where(array('typeid'=>$id))->count();
		if($res>0){
			echo 2;
		}else{
			$r = $award->delete($id);
			echo $r ? 1 : 0;
		}
	}


}